<?php
require_once 'Zend/Loader/Autoloader.php';      
/**
* Loader class, used for auto loading classes & files
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) 
* @license New BSD License
* @version 0.1
* @category Shuffler
* @package Shuffler_Loader
*/
class Shuffler_Loader_Autoloader extends Zend_Loader_Autoloader
{
    /**
    * Tries to call Zend_Loader::loadClass and return true, fails gracefully and returns false
    * if file not found, throws exception if class not found but file did exist,
    * used for conditionally overriding / vs code generation in application code
    * 
    * @throws exception if file was found but class is not defined
    */
    public static function autoload( $class, $dirs = null )
    {
        if ( class_exists($class, false) || interface_exists($class, false) )
        {
            return true;
        }
        $file = str_replace('_', '/', $class) . '.php';
        // checking hte include path
        $pathArray = explode( PATH_SEPARATOR, get_include_path() );
        $found = false;
        if( file_exists( $file ) )
        {
            $found = true;;
        }
        foreach( $pathArray as $path )
        {
            if( file_exists( $path . '/' . $file ) )
            {
                $found = true;
            }
        }
        if( !$found )
        {
            return false;
        }
        
        require_once $file;
        if (!class_exists($class, false) && !interface_exists($class, false))
        {
            throw new Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
        }
        return true;
    }
    
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * Constructor
     *
     * Registers instance with spl_autoload stack
     * 
     * @return void
     */
    protected function __construct()
    {
        spl_autoload_register(array(__CLASS__, 'autoload'));
        $this->_internalAutoloader = array($this, '_autoload');
    }
    
    protected function _autoload($class)
    {
        $callback = $this->getDefaultAutoloader();
        if ($this->suppressNotFoundWarnings()) {
            try {
                call_user_func($callback, $class);
                return $class;
            } catch (Zend_Exception $e) {
                return false;
            }
        }
        return call_user_func($callback, $class);
    }
}